1 /*
2 * Title: S/MIME Project
3 * Description: S/MIME email sending capabilities
4 * @Author Vladimir Radisic
5 * @Version 2.0.1
6 */
7
8
9 package org.webdocwf.util.smime.util;
10
11
12 import java.math.BigInteger;
13
14
15 /***
16 * DERLengthSearcher is used for searching the content length of inner DER object,
17 * and for counting the number of length octets in that DER object. This data
18 * can be used later for counting length of the whole inner DER encoded
19 * object as:<BR>
20 * 1 + number of length octets + number of content octets
21 */
22 public class DERLengthSearcher {
23
24 /***
25 * Number of content octets for specified DER object
26 */
27 private int lengthContent;
28
29 /***
30 * Number of length octets for specified DER object
31 */
32 private int lengthLength;
33
34 /***
35 * Construction with the position (index) of the inner DER encoded object,
36 * which length is required, and with the structured outer DER encoded object
37 * represented as byte array
38 * @param startElement0 position in byte array where inner DER object begines
39 * (Start element is always DER object type identifier)
40 * @param array0 structured DER encoded object in which is counted the length
41 * of particular inner DER object
42 */
43 public DERLengthSearcher(int startElement0, byte[] array0) {
44 this.newInitialization(startElement0, array0);
45 }
46
47 /***
48 * Initialization of the next length searching
49 * @param startElement0 starting element for next searching
50 * @param array0 structured DER encoded object in which the length of the
51 * particular iner DER object is being counted
52 */
53 public void newInitialization(int startElement0, byte[] array0) // Start element is DER object type identifier
54 {
55 int numBytes;
56 byte[] temp;
57
58 if ((array0[startElement0 + 1] & 128) == 128) // If long form of length octets is present
59 {
60 numBytes = array0[startElement0 + 1] & 0x7F;
61 if ((array0[startElement0 + 2] & 128) == 128) // For BigInteger (for example) 0x80 is negativ number (positiv must be written like 0x0080)
62 {
63 temp = new byte[numBytes + 1];
64 temp[0] = 0;
65 for (int i = 0; i != numBytes; i++)
66 temp[i + 1] = array0[startElement0 + 2 + i];
67 } else {
68 temp = new byte[numBytes];
69 for (int i = 0; i != numBytes; i++)
70 temp[i] = array0[startElement0 + 2 + i];
71 }
72 lengthContent = new BigInteger(temp).intValue();
73 lengthLength = numBytes + 1;
74 } else {
75 lengthContent = array0[startElement0 + 1];
76 lengthLength = 1;
77 }
78 }
79
80 /***
81 * Returns the number of the content octets in particular DER encoded object
82 * @return Number of content octets
83 */
84 public int getLengthtDERContentPart() {
85 return lengthContent;
86 }
87
88 /***
89 * Returns the number of the length octets in particular DER encoded object
90 * @return Number of length octets
91 */
92 public int getLengthtDERLengthPart() {
93 return lengthLength;
94 }
95 }
96
This page was automatically generated by Maven